package xianchenglock1;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @功能
 * @创建时间 2019/12/18
 */
public class MyContainer2 {
    volatile List lists = new ArrayList();
    public void add(Object o){
        lists.add(o);
    }
    public int size(){
        return lists.size();
    }

    public static void main(String[] args) {
        MyContainer2 c = new MyContainer2();
        new Thread(()->{
            for(int i=0;i<10;i++){
                c.add(new Object());
                System.out.println("add"+i);
                try {
                    TimeUnit.SECONDS.sleep(1);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                if(c.size()==5){
                    new Thread(()->{

                        System.out.println("del"+c.size());
                    },"t2").start();
                }
            }
        },"t1").start();




            }

    }

